import { request } from '@/service/request';

/**
 * 查询采集规则列表
 */
export function fetchCollectionRuleList(params?: Api.Dataplatform.CollectionRuleSearchParams) {
  return request<Api.Dataplatform.CollectionRuleList>({
    url: '/dataplatform/file-collection/rule/list',
    method: 'get',
    params
  });
}

/**
 * 查询采集规则详情
 */
export function fetchCollectionRuleDetail(id: number) {
  return request<Api.Dataplatform.CollectionRuleDetail>({
    url: `/dataplatform/file-collection/rule/${id}`,
    method: 'get'
  });
}

/**
 * 新增采集规则
 */
export function addCollectionRule(data: Api.Dataplatform.CollectionRuleEdit) {
  return request<number>({
    url: '/dataplatform/file-collection/rule',
    method: 'post',
    data
  });
}

/**
 * 修改采集规则
 */
export function updateCollectionRule(data: Api.Dataplatform.CollectionRuleEdit) {
  return request({
    url: '/dataplatform/file-collection/rule',
    method: 'put',
    data
  });
}

/**
 * 删除采集规则
 */
export function deleteCollectionRule(id: number) {
  return request({
    url: `/dataplatform/file-collection/rule/${id}`,
    method: 'delete'
  });
}

/**
 * 启用采集规则
 */
export function enableCollectionRule(id: number) {
  return request({
    url: `/dataplatform/file-collection/rule/${id}/enable`,
    method: 'put'
  });
}

/**
 * 禁用采集规则
 */
export function disableCollectionRule(id: number) {
  return request({
    url: `/dataplatform/file-collection/rule/${id}/disable`,
    method: 'put'
  });
}

/**
 * 手动触发采集任务
 */
export function triggerCollectionTask(ruleId: number) {
  return request<number>({
    url: `/dataplatform/file-collection/rule/${ruleId}/trigger`,
    method: 'post'
  });
}

/**
 * 查询采集任务列表
 */
export function fetchCollectionTaskList(params?: Api.Dataplatform.CollectionTaskSearchParams) {
  return request<Api.Dataplatform.CollectionTaskList>({
    url: '/dataplatform/file-collection/task/list',
    method: 'get',
    params
  });
}

/**
 * 查询采集任务详情
 */
export function fetchCollectionTaskDetail(id: number) {
  return request<Api.Dataplatform.CollectionTaskDetail>({
    url: `/dataplatform/file-collection/task/${id}`,
    method: 'get'
  });
}

/**
 * 停止采集任务
 */
export function stopCollectionTask(id: number) {
  return request({
    url: `/dataplatform/file-collection/task/${id}/stop`,
    method: 'put'
  });
}

/**
 * 重跑采集任务
 */
export function retryCollectionTask(id: number) {
  return request<number>({
    url: `/dataplatform/file-collection/task/${id}/retry`,
    method: 'post'
  });
}

/**
 * 查询文件记录列表
 */
export function fetchFileRecordList(params: Api.Dataplatform.FileRecordSearchParams) {
  return request<Api.Dataplatform.FileRecordList>({
    url: '/dataplatform/file-collection/file/list',
    method: 'get',
    params
  });
}

/**
 * 查询文件记录详情
 */
export function fetchFileRecordDetail(id: number) {
  return request<Api.Dataplatform.FileRecordDetail>({
    url: `/dataplatform/file-collection/file/${id}`,
    method: 'get'
  });
}

/**
 * 下载文件
 */
export function downloadFile(id: number) {
  return request<string>({
    url: `/dataplatform/file-collection/file/${id}/download`,
    method: 'get'
  });
}

/**
 * 查询映射规则列表
 */
export function fetchMappingRuleList(params?: Api.Dataplatform.MappingRuleSearchParams) {
  return request<Api.Dataplatform.MappingRuleList>({
    url: '/dataplatform/file-collection/mapping/list',
    method: 'get',
    params
  });
}

/**
 * 查询映射规则详情
 */
export function fetchMappingRuleDetail(id: number) {
  return request<Api.Dataplatform.MappingRuleDetail>({
    url: `/dataplatform/file-collection/mapping/${id}`,
    method: 'get'
  });
}

/**
 * 新增映射规则
 */
export function addMappingRule(data: Api.Dataplatform.MappingRuleEdit) {
  return request<number>({
    url: '/dataplatform/file-collection/mapping',
    method: 'post',
    data
  });
}

/**
 * 修改映射规则
 */
export function updateMappingRule(data: Api.Dataplatform.MappingRuleEdit) {
  return request({
    url: '/dataplatform/file-collection/mapping',
    method: 'put',
    data
  });
}

/**
 * 删除映射规则
 */
export function deleteMappingRule(id: number) {
  return request({
    url: `/dataplatform/file-collection/mapping/${id}`,
    method: 'delete'
  });
}

/**
 * 检测文件字段
 */
export function detectFileFields(file: File, fileType: string) {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('fileType', fileType);
  
  return request<Api.Dataplatform.FieldInfo[]>({
    url: '/dataplatform/file-collection/mapping/detect-fields',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 查询目标表列表
 */
export function fetchTargetTables(database: string) {
  return request<string[]>({
    url: '/dataplatform/file-collection/mapping/target-tables',
    method: 'get',
    params: { database }
  });
}

/**
 * 获取表字段信息
 */
export function fetchTableFields(database: string, table: string) {
  return request<Api.Dataplatform.FieldInfo[]>({
    url: '/dataplatform/file-collection/mapping/table-fields',
    method: 'get',
    params: { database, table }
  });
}

/**
 * 验证映射规则
 */
export function validateMappingRule(data: Api.Dataplatform.MappingRuleEdit) {
  return request<Api.Dataplatform.ValidationResult>({
    url: '/dataplatform/file-collection/mapping/validate',
    method: 'post',
    data
  });
}

/**
 * 查询任务统计信息
 */
export function fetchTaskStatistics(ruleId?: number) {
  return request<Record<string, any>>({
    url: '/dataplatform/file-collection/task/statistics',
    method: 'get',
    params: { ruleId }
  });
}

/**
 * 查询任务执行趋势
 */
export function fetchTaskTrend(ruleId?: number, days: number = 7) {
  return request<Array<Record<string, any>>>({
    url: '/dataplatform/file-collection/task/trend',
    method: 'get',
    params: { ruleId, days }
  });
}

// Alias functions for consistency
export const fetchTaskDetail = fetchCollectionTaskDetail;
export const stopTask = stopCollectionTask;
export const retryTask = retryCollectionTask;

/**
 * 上传单个非结构化文件
 */
export function uploadUnstructuredFile(file: File, data?: Record<string, any>) {
  const formData = new FormData();
  formData.append('file', file);
  if (data) {
    Object.keys(data).forEach(key => {
      formData.append(key, data[key]);
    });
  }
  
  return request<number>({
    url: '/dataplatform/file-collection/unstructured/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 批量上传非结构化文件
 */
export function uploadUnstructuredFiles(files: File[], data?: Record<string, any>) {
  const formData = new FormData();
  files.forEach(file => {
    formData.append('files', file);
  });
  if (data) {
    Object.keys(data).forEach(key => {
      formData.append(key, data[key]);
    });
  }
  
  return request<number[]>({
    url: '/dataplatform/file-collection/unstructured/upload/batch',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 从指定来源采集非结构化文件
 */
export function collectUnstructuredFiles(sourceType: string, sourceConfig: string) {
  return request({
    url: '/dataplatform/file-collection/unstructured/collect',
    method: 'post',
    params: { sourceType, sourceConfig }
  });
}

/**
 * 查询非结构化文件列表
 */
export function fetchUnstructuredFileList(params?: Api.Dataplatform.UnstructuredFileSearchParams) {
  return request<Api.Dataplatform.UnstructuredFileList>({
    url: '/dataplatform/file-collection/unstructured/list',
    method: 'get',
    params
  });
}

/**
 * 查询非结构化文件详情
 */
export function fetchUnstructuredFileDetail(id: number) {
  return request<Api.Dataplatform.UnstructuredFileDetail>({
    url: `/dataplatform/file-collection/unstructured/${id}`,
    method: 'get'
  });
}

/**
 * 删除非结构化文件
 */
export function deleteUnstructuredFile(id: number) {
  return request({
    url: `/dataplatform/file-collection/unstructured/${id}`,
    method: 'delete'
  });
}

/**
 * 批量删除非结构化文件
 */
export function deleteUnstructuredFiles(ids: number[]) {
  return request({
    url: '/dataplatform/file-collection/unstructured/batch',
    method: 'delete',
    data: ids
  });
}

/**
 * 获取非结构化文件下载URL
 */
export function getUnstructuredFileDownloadUrl(id: number) {
  return request<string>({
    url: `/dataplatform/file-collection/unstructured/${id}/download`,
    method: 'get'
  });
}

/**
 * 提取文件元数据
 */
export function extractFileMetadata(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  
  return request<Record<string, any>>({
    url: '/dataplatform/file-collection/unstructured/metadata',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 查询采集历史文件记录列表
 */
export function fetchHistoryFileRecords(params?: Record<string, any>) {
  return request<any>({
    url: '/dataplatform/file-collection/history/file-records',
    method: 'get',
    params
  });
}

/**
 * 查询采集历史文件记录详情
 */
export function fetchHistoryFileRecordDetail(id: number) {
  return request<any>({
    url: `/dataplatform/file-collection/history/file-records/${id}`,
    method: 'get'
  });
}

/**
 * 预览文件数据
 */
export function previewHistoryFileData(id: number, limit?: number) {
  return request<any>({
    url: `/dataplatform/file-collection/history/file-records/${id}/preview`,
    method: 'get',
    params: { limit }
  });
}

/**
 * 下载历史文件
 */
export function downloadHistoryFile(id: number) {
  return request<string>({
    url: `/dataplatform/file-collection/history/file-records/${id}/download`,
    method: 'get'
  });
}

/**
 * 获取文件采集统计
 */
export function fetchHistoryFileStatistics(params?: Record<string, any>) {
  return request<Record<string, any>>({
    url: '/dataplatform/file-collection/history/file-records/statistics',
    method: 'get',
    params
  });
}

/**
 * 查询采集错误记录列表
 */
export function fetchCollectionErrors(params?: Record<string, any>) {
  return request<any>({
    url: '/dataplatform/file-collection/history/errors',
    method: 'get',
    params
  });
}

/**
 * 查询采集错误记录详情
 */
export function fetchCollectionErrorDetail(id: number) {
  return request<any>({
    url: `/dataplatform/file-collection/history/errors/${id}`,
    method: 'get'
  });
}

/**
 * 获取错误统计
 */
export function fetchErrorStatistics(params?: Record<string, any>) {
  return request<Record<string, any>>({
    url: '/dataplatform/file-collection/history/errors/statistics',
    method: 'get',
    params
  });
}


/**
 * 获取监控统计数据
 */
export function fetchMonitorStatistics() {
  return request<any>({
    url: '/dataplatform/file-collection/monitor/statistics',
    method: 'get'
  });
}

/**
 * 获取任务性能指标
 */
export function fetchTaskMetrics(taskId: number) {
  return request<any>({
    url: `/dataplatform/file-collection/monitor/task/${taskId}/metrics`,
    method: 'get'
  });
}

/**
 * 获取全局性能指标
 */
export function fetchGlobalMetrics() {
  return request<any>({
    url: '/dataplatform/file-collection/monitor/global/metrics',
    method: 'get'
  });
}

/**
 * 获取任务监控概览
 */
export function fetchTaskOverview() {
  return request<any>({
    url: '/dataplatform/file-collection/monitor/task/overview',
    method: 'get'
  });
}

/**
 * 获取文件监控概览
 */
export function fetchFileOverview() {
  return request<any>({
    url: '/dataplatform/file-collection/monitor/file/overview',
    method: 'get'
  });
}

/**
 * 获取数据监控概览
 */
export function fetchDataOverview() {
  return request<any>({
    url: '/dataplatform/file-collection/monitor/data/overview',
    method: 'get'
  });
}

/**
 * 获取系统资源监控
 */
export function fetchSystemResources() {
  return request<any>({
    url: '/dataplatform/file-collection/monitor/system/resources',
    method: 'get'
  });
}
